package com.burgeon.backend.SHOPBACKEND.rest;
/**
 * 社群营销成交 top1
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class QuerySocialMarketingChengjiaoTop1 extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QuerySocialMarketingChengjiaoTop1(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public String getData(String[] id) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(id), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        //System.err.println(jsonArray);

        ArrayList<String> data = new ArrayList<String>();
        for (int i=0;i<jsonArray.length();i++){
            data.add(jsonArray.getJSONArray(i).getString(0));
        }

        Map<String, Integer> map = new HashMap<>();
        int maxcount = 0;
        String maxEmp = "-";

        for (String empid :data){
            int i = 1;
            if (map.get(empid) != null){
                i = map.get(empid) + 1;
            }
            // 个人觉得这一步巧：精髓
            if (i>maxcount){
                maxcount = i;
                maxEmp = empid;
            }
            map.put(empid, i);
        }
        System.out.println("重复数据的个数：" + map.toString());
        return maxEmp;
    }

    private MultiValueMap Data(String[] arr) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        //columns.add("ID");
        columns.add("'SALESREP_ID;NAME'");



        params.put("columns", columns);
        params.put("table", "YC_RETAILITEM");//表名

        params.put("count","true");


        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        expr1.put("column", "'SALESREP_ID'");
        expr1.put("condition", "\" IN ("+ toString(arr)+")\"");

        expr2.put("column", "'YC_RETAIL_ID;STATUS'");
        expr2.put("condition", "2");

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);
        params.put("params", paramss);
        return DATA("Query", params,account,password);
    }
    private String toString(String[] data){

        return Arrays.toString(data).replace("\"","'")
                .replace("[","")
                .replace("]","")
                .replace(" ", "");

    }
    public static void main(String[] args) {
        String[] arr = new String[]{"7018","5154","7176"};
        QuerySocialMarketingChengjiaoTop1 queryMail = new QuerySocialMarketingChengjiaoTop1("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(queryMail.getData(arr));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
